package org.shangrila.app.sys.mb;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.shangrila.app.sys.dao.SysDao;
import org.shangrila.app.sys.eo.SysOrganization;
import org.shangrila.app.sys.eo.SysUser;
import org.shangrila.comm.dao.DbAccess;

@Named
@SessionScoped
public class SysOrganMB implements Serializable {

	private static final long serialVersionUID = 1L;
	private TreeNode root;
    private TreeNode selectedNode;
    private TreeNode[] selectedNodes;
     
    private OrganNodeData selectedNodeData;
    private OrganNodeData addNodeData = new OrganNodeData();
    
	@PostConstruct
    public void init() { 	
    	SysDao dao = new SysDao();
    	List<SysOrganization> list = dao.findSysOrganizationAll();     	    	
    	OrganNodeData  rootTND = new OrganNodeData(list.get(0).getId(),list.get(0).getName(),"0",list.get(0).getActive(),0);   	
    	root = new DefaultTreeNode(rootTND, null);
    	TreeNode root0 = new DefaultTreeNode(rootTND, root); 
    	generateTreeData(list.get(0),list,root0,1);  
    } 

	@SuppressWarnings("unused")
	public void generateTreeData(SysOrganization pnode,List<SysOrganization> list,TreeNode tnode,int pint){
		for(SysOrganization so:list){
			if(so.getId() != so.getPid() && pnode.getId() == so.getPid()){
				//System.out.println("Children "+pint+"--"+so.getName());
	    		TreeNode tn = new DefaultTreeNode(new OrganNodeData(so.getId(),so.getName(),"0",so.getActive(),so.getId()), tnode);
				if("1".equals(so.getType())) { //叶子节点
					//System.out.println("Leaf "+pint+"--"+so.getName());					
					for(SysUser su:so.getSysUsers()){    		
			    		TreeNode tnUser = new DefaultTreeNode("usernodes",new OrganNodeData(so.getId(),su.getName(),"1",so.getActive(),su.getId()), tn);
					}					
				}
				else{ //非叶子节点
					generateTreeData(so,list,tn,++pint);
				}
			}
		}
	}
    
    public void showInfo() {
        if(selectedNode != null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "当前选择", ((OrganNodeData)selectedNode.getData()).getName());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
    public String showInfos() {
        if(selectedNode != null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "当前选择", selectedNode.getData().toString());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        return null;
    }

    public void displaySelectedSingle() {
        if(selectedNode != null) {
        	selectedNodeData = (OrganNodeData)selectedNode.getData();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "当前选择", ((OrganNodeData)selectedNode.getData()).getName());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
     
    public void displaySelectedMultiple(TreeNode[] nodes) {
        if(nodes != null && nodes.length > 0) {
            StringBuilder builder = new StringBuilder();
 
            for(TreeNode node : nodes) {
                builder.append(node.getData().toString());
                builder.append("<br />");
            }
 
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "当前选择", builder.toString());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
         
    public void deleteNode() { 
        selectedNode.getChildren().clear();
        selectedNode.getParent().getChildren().remove(selectedNode);
        selectedNode.setParent(null);         
        selectedNode = null;
    }

    public String addAction() {
        if(selectedNode != null) {
           	SysOrganization so  = new SysOrganization();
        	OrganNodeData tmp = ((OrganNodeData)selectedNode.getData());
        	so.setId(tmp.getOrgid());
        	so.setName(tmp.getName());
        	so.setDescription(so.getName());
        	so.setFullname(so.getName());
        	so.setType(tmp.getDtype());
        	so.setActive(tmp.getDactive());
        	
        	DbAccess db = new DbAccess();
        	db.beginTransaction();
        	db.insert(so);
        	db.commitTransaction();

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "新增提交", ((OrganNodeData)selectedNode.getData()).getName());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        return null;
    }
 
    public String editAction() {
        if(selectedNode != null) { 
        	SysOrganization so  = new SysOrganization();
        	OrganNodeData tmp = ((OrganNodeData)selectedNode.getData());
        	so.setId(tmp.getOrgid());
        	so.setName(tmp.getName());
        	so.setDescription(so.getName());
        	so.setFullname(so.getName());
        	so.setType(tmp.getDtype());
        	so.setActive(tmp.getDactive());
        	
        	DbAccess db = new DbAccess();
        	db.beginTransaction();db.update(so);
        	db.commitTransaction();
        	
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "修改提交", ((OrganNodeData)selectedNode.getData()).getName());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        return null;
    }
    
    public void onRowEdit(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("提交编辑", ((TreeNode) event.getObject()).toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
     
    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("取消编辑", ((TreeNode) event.getObject()).toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
    public TreeNode getRoot() {
        return root;
    }
 
    public TreeNode getSelectedNode() {
        return selectedNode;
    }
 
    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }
 
    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }
 
    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }
 
	public OrganNodeData getSelectedNodeData() {
		return selectedNodeData;
	}

	public void setSelectedNodeData(OrganNodeData selectedNodeData) {
		this.selectedNodeData = selectedNodeData;
	}

	public OrganNodeData getAddNodeData() {
		return addNodeData;
	}

	public void setAddNodeData(OrganNodeData addNodeData) {
		this.addNodeData = addNodeData;
	}
    
}